import { debounce } from '@/utils'
export function toast(
  query: string | UniApp.ShowToastOptions,
  icon: 'none' | 'success' | 'loading' = 'none'
) {
  const options: UniApp.ShowToastOptions =
    typeof query === 'string' ? { title: query, icon, duration: 2000 } : query

  return setTimeout(() => {
    uni.showToast(options)
  }, 200)
}
toast.success = (str: string) => toast(str, 'success')

export function loading(query: string | UniApp.ShowToastOptions = '加载中') {
  const options =
    typeof query === 'string' ? { title: query, mask: true } : query
  return uni.showLoading(options)
}

export function showModal(query: string | UniApp.ShowModalOptions) {
  const defOpt = {
    title: '温馨提示',
    confirmColor: '#2FB3FF',
    cancelText: '取消',
    confirmText: '确定'
  }
  const options = typeof query === 'string' ? { content: query } : query
  console.log(options, '======')
  return new Promise((resolve, reject) => {
    const opt = { ...defOpt, ...options }
    uni.showModal({
      ...opt,
      success: (res) => {
        res.confirm ? resolve(res) : reject(res)
      },
      fail: (err) => {
        resolve(err)
      }
    })
  })
}

export function hideLoading() {
  debounce(uni.hideLoading)
}

export function hideToast() {
  uni.hideToast()
}

export default {
  toast,
  loading,
  showModal,
  hideLoading,
  hideToast
}
